<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		data() {
			return buildFormData({
				userCacheKey:'obj-zb-tindexAdjust-user-cache',
				fileId:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			//字表删除时执行
			verifySubDel(index,editor){
				let _that = this
				if(editor.groupKey == "title_before"){
					if(_that.subEditorObj["title_after"] && _that.subEditorObj["title_after"].workList.length){
						_that.subEditorObj["title_after"].workList.splice(index,1)
					}
				}
			},
			//跳转到原指标携带参数
			getWorkParam(){
				let _that = this
				return {
					year: _that.$refs.autoform.formdata.year,
					userId: _that.$refs.autoform.formdata.userId,
					posId: _that.$refs.autoform.formdata.posId,
					editId: _that.fileId
				}
			},
			//岗位选中赋值相关信息
			onSelectPos(item){
				let _that = this
				_that.$refs.autoform.formdata.instName = item.instName
				_that.$refs.autoform.formdata.parentInstId = item.parentInstId
				_that.$refs.autoform.formdata.parentInstName = item.parentInstName
			},
			//选择负责任改变岗位名称空间类型
			changePosType(id){
				let _that = this
				_that.om.request(
					'orgInstPosUserOfficial/getPosComboboxByUser',
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor('title_userInfo',"posId","type","selectbox")
					_that.$refs.autoform.changeEditor('title_userInfo',"posId","data",configSelectData(res,"id","posName"))
					_that.$refs.autoform.changeEditor('title_userInfo',"posId","onSelect","onSelectPos")
					_that.$refs.autoform.initSelectVal('title_userInfo',"posId")
				})
			},
			//年份选择执行
			onSelectYear(){
				let _that = this
				_that.$refs.autoform.changeEditor("title_userInfo","userId","type","linkbox")
				_that.$refs.autoform.changeEditor('title_userInfo',"userId",{
					link:'../../public/pageSearch/pageSearch',
					linkParam: {
						other: JSON.stringify({
							zposId: _that.$refs.autoform.formdata.zposId
						}),
						initload: true,
						namefield:'username',
						searchfield:'q',
						url:'tindexAdjust/getUserPage',
						cachename: _that.userCacheKey,
					}
				})
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				_that.fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if(fileId){
					ajaxArr.push({
						url:'tindexAdjust/getInfo',
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					let num = moment().get('year');
					let yearList = [
						{
							title:num,
							value:num
						},
						{
							title:num+1,
							value:num+1
						}
					]
					changeConfigAttr("title_after","subTitle",'点击进入编辑',mainData)
					changeEditor("topinfo","year","data",configSelectData(yearList,"title","value"),mainData)
					if(_that.subEditorObj['title_before']){
						let attr = {
							showAdd:false,
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['title_before']
						if(hasEditor("title_before","indexId",configData)){
							attr.titleTemplate ='<%=indexName || "未填写"%>'
						} 
						if(hasEditor("title_before","orderNum",configData)){
							attr.subtitle1Template ='<%=orderNum || "未填写"%>'
						} 
						changeEditor("title_before","beforeStr",attr,mainData)
					}
					if(_that.subEditorObj['title_after']){
						let attr = {
							showAdd:false,
						}
						let configData = _that.subConfigData['title_after']
						if(hasEditor("title_after","indexName",configData)){
							attr.titleTemplate ='<%=indexName || "未填写"%>'
						} 
						if(hasEditor("title_after","orderNum",configData)){
							attr.subtitle1Template ='<%=orderNum || "未填写"%>'
						} 
						changeEditor("title_after","afterStr",attr,mainData)
					}
					if(_that.subEditorObj['title_new']){
						let attr = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['title_new']
						if(hasEditor("title_new","indexId",configData)){
							attr.titleTemplate ='<%=indexName || "未填写"%>'
						} 
						changeEditor("title_new","newStr",attr,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id","parentInstId")
						for(let index in res[1].value.tindexAdjust){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.tindexAdjust[index])
								_that.$set(_that.formdata,index,res[1].value.tindexAdjust[index])
							}
						}
						if(hasEditor('title_before','beforeStr',mainData)){
							let list1 = _that._.cloneDeep(res[1].value.list1)
							_that.subEditorObj["title_before"].workList = list1
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"beforeStr",JSON.stringify(list1))
							_that.$set(_that.formdata,"beforeStr",JSON.stringify(list1))
						}
						if(hasEditor('title_after','afterStr',mainData)){
							let list3 = _that._.cloneDeep(res[1].value.list3)
							_that.subEditorObj["title_after"].workList = list3
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"afterStr",JSON.stringify(list3))
							_that.$set(_that.formdata,"afterStr",JSON.stringify(list3))
						}
						if(hasEditor('title_new','newStr',mainData)){
							let list2 = _that._.cloneDeep(res[1].value.list2)
							_that.subEditorObj["title_new"].workList = list2
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"newStr",JSON.stringify(list2))
							_that.$set(_that.formdata,"newStr",JSON.stringify(list2))
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("topinfo","year")
						if(fileId){
							_that.onSelectYear()
							if(_that.subEditorObj['title_before']){
								let attr = {
									showAdd:true
								}
								let configData = _that.subConfigData['title_brfore']
								_that.$refs.autoform.changeEditor("title_before","beforeStr",attr)
							}
							_that.changePosType(_that.$refs.autoform.formdata.userId)
						}
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				if(_that.subEditorObj['title_new']){
					for(let item of _that.subEditorObj['title_new'].workList){
						item.targetValueName = item.logicRelationshipName + item.targetValueCode + item.indexUnitName
					}
					_that.formdata.newStr = JSON.stringify(_that.subEditorObj['title_new'].workList)
				}
				_that.om.formSave("tindexAdjust/addTindexAdjust",_that.formdata,_that)
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				if(_that.subEditorObj['title_before'] && _that.subEditorObj["title_after"]){
					_that.subEditorObj['title_before'].workList.forEach((item, index) => {
						if(!_that.subEditorObj['title_after'].workList[index]){
							_that.subEditorObj['title_after'].workList.push(item)
						}
					});
				}
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							if(_that.subEditorObj['title_before']){
								let attr = {
									showAdd:true
								}
								let configData = _that.subConfigData['title_brfore']
								_that.$refs.autoform.changeEditor("title_before","beforeStr",attr)
							}
							_that.changePosType(d.id)
						}
						uni.removeStorage({
							key:_that.userCacheKey
						})
					}
				})
			}
		},
	}
</script>

<style>
</style>
       